package com.zhitong.money.mapper;

import com.zhitong.money.entity.Position;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;
import java.util.Map;

@Mapper
public interface PositionMapper {
    @Results(id = "positionMap",value = {
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "did",property = "did"),
            @Result(column = "name",property = "name"),
            @Result(column = "leaderId",property = "leaderId"),
            @Result(column = "description",property = "description"),
            @Result(column = "level",property = "level"),
            @Result(column = "status",property = "status"),
    })
    @Select("select * from position")
    public List<Position> findAllPosition();

    @ResultMap("positionMap")
    @Select({
            "<script>",
            "select * from position",
            "<where>",
            "<if test='did!=null and did!=\"\"'>and did=#{did}</if>",
            "<if test='name!=null and name!=\"\"'>and name like concat(concat('%',#{name},'%'))</if>",
            "<if test='leaderId!=null and leaderId!=\"\"'>and leaderId=#{leaderId}</if>",
            "<if test='level!=null and level!=\"\"'> and level=#{level}</if>",
            "<if test='status!=null and status!=\"\"'>and status=#{status}</if>",
            "</where>",
            "</script>"
    })
    public List<Position> findAllByMap(Map<String,Object> map);


    @Select({
            "<script>",
            "select id from position",
            "<where>",
            "<if test='did!=null and did!=\"\"'>and did=#{did}</if>",
            "<if test='name!=null and name!=\"\"'>and name like concat(concat('%',#{name},'%'))</if>",
            "<if test='leaderId!=null and leaderId!=\"\"'>and leaderId=#{leaderId}</if>",
            "<if test='level!=null and level!=\"\"'> and level=#{level}</if>",
            "<if test='status!=null and status!=\"\"'>and status=#{status}</if>",
            "</where>",
            "</script>"
    })
    public List<String> findIdsByMap(Map<String,Object> map);



    @Select("select id from position where leaderId = #{lid}")
    public List<String> findPositionByLeaderId(String lid);

    //根据领导id查询职位ids
    @Select({
            "<script>",
            "select id from position",
            " where leaderId in",
            "<foreach item='item' collection='lids' separator=',' open='(' close=')' index=''>",
            "#{item}",
            "</foreach>",
            "</script>"
    })
    public List<String> fidPidsByLids(List<String> lids);

    //查询除ids里面的id外
    @ResultMap("positionMap")
    @Select({
            "<script>",
            "select * from position",
            " where id not in",
            "<foreach item='item' collection='ids' separator=',' open='(' close=')' index=''>",
            "#{item}",
            "</foreach>",
            "</script>"
    })
    public List<Position> fidPositionsByNoInIds(List<String> ids);

    //查询下属员工ids(除懂事长和副董事长)
    @Select("select id from position where did !='01'")
    public List<String> findSubPids();


    @ResultMap("positionMap")
    @Select("select * from position where id =#{id}")
    public Position findPositionById(String id);

    @ResultMap("positionMap")
    @Select("select * from position where id in (select pid from employee where tellerId=#{tid})")
    public Position findPositionByTid(String tid);

    @ResultMap("positionMap")
    @Select("select * from position where name =#{name}")
    public Position findPositionByName(String name);

    @Select("select id from position where did=#{did}")
    public List<String> findIdsByDid(String did);

    @Insert("insert into position values(#{id},#{did},#{name},#{leaderId},#{description},#{level},#{status})")
    public void addPosition(Position position);


    @Delete("delete from position where id = #{id}")
    public int delPositionById(String id);

    @Delete({
            "<script>",
                "delete from position",
                " where id in",
                "<foreach item='item' collection='ids' separator=',' open='(' close=')' index=''>",
                "#{item}",
                "</foreach>",
            "</script>"
    })
    public int delPositionSByIds(List<String> ids);



    @Update({
            "<script>",
            "update position",
            "<trim prefix='set' suffixOverrides=','>",
            "<if test='did!=null and did!=\"\"'> did=#{did},</if>",
            "<if test='name!=null and name!=\"\"'> name=#{name},</if>",
            "<if test='leaderId!=null and leaderId!=\"\"'> leaderId=#{leaderId},</if>",
            "<if test='description!=null and description!=\"\"'> description=#{description},</if>",
            "<if test='level!=null and level!=\"\"'>level=#{level},</if>",
            "<if test='status!=null and status!=\"\"'> status=#{status},</if>",
            "</trim>",
            "where id = #{id}",
            "</script>"
    })
    public void updatePosition(Position position);

    @Update("update set leaderId='' where leaderId=#{lid}")
    public void updateLeaderIdToNullByLid(String Lid);

    @Update({
            "<script>",
            "update position",
            "<trim prefix='set' suffixOverrides=','>",
            "<if test='leaderId!=null and leaderId!=\"\"'> leaderId=#{leaderId},</if>",
            "<if test='did!=null and did!=\"\"'> did=#{did},</if>",
            "</trim>",
            "where id in",
            "<foreach item='item' collection='ids' separator=',' open='(' close=')' index=''>",
            "#{item}",
            "</foreach>",
            "</script>"
    })
    public void updatePositionByIds(Map<String,Object> map);



}
